#ifndef IOACTIONFORM_H
#define IOACTIONFORM_H

#include <QWidget>
#include <QThread>
#include <QTimer>
#include <Common/common.h>


namespace Ui {
class ioActionForm;
}

// PLC读取线程
class PLCReadThread : public QThread
{
    Q_OBJECT
public:
    explicit PLCReadThread(QObject *parent = nullptr);
    void run() override;
    void stop();

signals:
    void inputStateChanged(int inputPort, bool state);

private:
    bool m_stop = false;
};

class ioActionForm : public QWidget
{
    Q_OBJECT

public:
    explicit ioActionForm(QWidget *parent = nullptr);
    ~ioActionForm();

    bool initPLC();
    void closePLC();
    void populateCOMPorts();

    // 添加配置读取和保存函数
    void readPLCConfig();
    void savePLCConfig();
signals:
    // 添加清除数据信号
    void clearDataRequested();

private slots:
    void onInputStateChanged(int inputPort, bool state);



    // 添加COM端口改变的处理
    void onComPortChanged(int index);
    void on_Bt_get_clicked();

    void on_Bt_start_clicked();

public slots:
    void slotFrameInfo(const CFrameResult& data);
private:
    Ui::ioActionForm *ui;
    PLCReadThread *m_plcThread;
    QString m_savedComPort;
    QString m_outputType;
    QList<int> m_do;

private:
    QTimer *m_timer;
    bool m_isRunning;
    bool m_isOpen;
    // 添加以下槽函数声明
private slots:
    // 添加以下槽函数声明
    void onDOIndexChanged(int index);
    void onTimeout();
    void on_Bt_clear_clicked();
    void on_Bt_save_clicked();
};

#endif // IOACTIONFORM_H
